package com.qfjy.project.meeting;

import com.qfjy.common.utils.exception.MeetingGrabException;
import com.qfjy.common.utils.result.ResultCode;
import com.qfjy.common.utils.result.ResultJson;
import com.qfjy.entity.po.MeetingGrab;
import com.qfjy.entity.po.MeetingPub;
import com.qfjy.service.MeetingGrabService;
import com.qfjy.service.MeetingPubService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @ClassName MeetingPubController
 * @Description TODO
 * @Author guoweixin
 * @Date 2022/12/5
 * @Version 1.0
 */
@Controller
@RequestMapping("meetingPub")
@Slf4j
@Api(tags = {"会议发单管理"})
public class MeetingPubController {


    @Autowired
    private MeetingPubService meetingPubService;

    /**会议抢单业务逻辑*/
    @Autowired
    private MeetingGrabService meetingGrabService;
    /**
     * TODO 会议发布（微信）
     * @param meetingPub
     * @return
     */
    @PostMapping("addMeetingPub")
    @ResponseBody
    public ResultJson  addMeetingPub(MeetingPub meetingPub){
        int num=0;
        try {
             num = meetingPubService.insertSelective(meetingPub);
        }catch (Exception ex){
            log.info("会议发布失败{},{}",ex.getMessage(),meetingPub);
            return new ResultJson(num,ResultCode.EXCEPTION);
        }
        if(num<1){
            return new ResultJson(num, ResultCode.FAIL);
        }

        return new ResultJson(num,ResultCode.SUCCESS);
    }
    /**
     * TODO 会议发布--》我的会议
     */
    @ResponseBody
    @GetMapping("listByUid")
    @ApiOperation(value = "我发布的会议列表根据用户ID")
    @ApiImplicitParams({
            @ApiImplicitParam(name="uid",value = "用户ID",defaultValue = "327")
    })
    public ResultJson selectMeetingPubByUid(@RequestParam("uid") final int uid){

        List<MeetingPub> list=meetingPubService.selectMeetingPubByUid(uid);

        if(list.size()==0){
            return new ResultJson<>(null,ResultCode.NOT_DATA);
        }

        return new ResultJson<>(list,ResultCode.SUCCESS);
    }

    /**
     * TODO 会议发布--》我的会议列表--》选择讲者按钮进入页面
     * @param pid  发单id   url:  /meetingPub/chooseGrabListToPage
     */
    @GetMapping("chooseGrabListToPage")
    public String chooseGrabListToPage(@RequestParam("pid") String pid, HttpServletRequest request){

        request.setAttribute("pid",pid);

        return "meetingPub/grabListByPid";
    }


    /**
     * TODO 会议发布--》我的会议列表 选择讲者
     */
    @ApiOperation(value ="选择讲者" )
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pid",value = "发单id",defaultValue = "71ed80cb-8be8-4039-adb9-5aa566ca0dc6")
    })
    @ResponseBody
    @GetMapping("selectGrabListChooseByPid")
    public ResultJson selectGrabListChooseByPid(@RequestParam("pid") String pid){

        List<MeetingGrab> list= meetingGrabService.selectGrabListByPid(pid);

        return new ResultJson(list,ResultCode.SUCCESS);
    }

    /**
     * TODO 会议发布--》我的会议列表  选择讲者 就选你
     */
    @PostMapping("chooseGrabByUidAndPid")
    @ResponseBody
    public ResultJson chooseGrabByUidAndPid(@RequestParam("pid") String pid,
                                        @RequestParam("uid")int uid){
        try {
            int num = meetingGrabService.chooseGrabByUidAndPid(pid, uid);
        }catch (MeetingGrabException ex){
            log.error(ex.getMessage());
            return new ResultJson(0,ResultCode.FAIL);
        }
        return new ResultJson<>(1,ResultCode.SUCCESS);
    }
}
